#!/bin/bash
#
#Rockey2 install script
#

PRODUCT=Rockey2
script_path=`dirname "$0"`
echo
echo Installing $PRODUCT SDK...
echo

#Check whether the current user is root
if test $(id -ur) != 0; then
	echo
	echo "You should logon as root user!"
	echo
	exit -1
fi
get_confirm()
{
	echo "[yes or no]"
	while true
	do
		read X
		case "$X" in
			y | yes | Y | Yes | YES ) return 1;;
			n | no | N | No | NO ) 
				echo "Cancelled"
				return 0;;
			*) echo "Please enter yes or no";;
		esac
	done
}
if [ -f /usr/local/include/rockey2.h ] || [ -f /usr/local/lib/libJRockey2.so ] || [ -f /usr/local/lib/libRockey2.a ] || [ -f /usr/local/lib/libRockey2.so ]; then

	echo "The file Driver that this program was about to install already exists. Overwrite?"
	if get_confirm ; then
	   exit 1
	fi
																													    
fi

#Create the /usr/local/lib dir and copy libs to this dir
mkdir -p /usr/local/lib
mkdir -p /usr/local/include

cp -f $script_path/include/rockey2.h /usr/local/include

#Check the Linux system for 32-bit or 64-bit
OSV=`file /bin/ls | cut -c14-15`
if [ $OSV = 32 ]; then
    cp -f $script_path/api/api32/* /usr/local/lib/
fi
if [ $OSV = 64 ]; then
    cp -f $script_path/api/api64/* /usr/local/lib/
fi


chmod go+rx /usr/local/lib/lib*$PRODUCT*

#check whether enable SELinux and gcc version
GCCVER=`cat /proc/version | awk '{print $7}' | cut -d. -f 1`
if [ $GCCVER = "4" ]; then
	if [ -f /etc/sysconfig/selinux ]; then
		grep "SELINUX=disable" /etc/sysconfig/selinux
		if [ $? == 1 ]; then
			chcon -t texrel_shlib_t /usr/local/lib/lib*$PRODUCT.so*>/dev/null
		fi
	fi
fi

if [ -f /etc/hotplug/usb.usermap ];then
	grep -l "ft.map" /etc/hotplug/usb.usermap>/dev/null
	if [ $? = 1 ];then
		echo "ft.map 0x0000 0x096e 0x0000 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000000">>/etc/hotplug/usb.usermap
	fi
fi

if [ -d /etc/hotplug/usb/ ];then
	cp -f $script_path/ft.map /etc/hotplug/usb/
fi

if [ -d /etc/udev/rules.d/ ]; then
	cp $script_path/ft.rules /etc/udev/rules.d/
	touch /etc/udev/rules.d/ft_H.rules
	echo "SUBSYSTEM==\"usb\",ATTRS{idVendor}==\"096e\",MODE==\"0666\"">/etc/udev/rules.d/ft_H.rules
fi

grep -q "/usr/local/lib" /etc/ld.so.conf 2>/dev/null
if [ $? != 0 ]; then
	cp /etc/ld.so.conf /etc/ld.so.conf.bak 2>/dev/null
	echo "/usr/local/lib" >> /etc/ld.so.conf
fi

/sbin/ldconfig 2>/dev/null

echo Install finished!

